﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using Assesment.Library;
using Assesment.Library.DomainObject.Administration.UserManagement;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;

namespace Assesment.Library.DataAccess.Administration.UserManagement
{

    public partial class recMenuRightsRepo
    {
        public bool SaveDelete(List<recMenuRights> domains)
        {
            bool result = false;

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                    SqlCommand cmdDelete = conn.CreateCommand();
                    cmdDelete.CommandText = " delete from [master].recMenuRights where RoleId = @RoleID ";
                    cmdDelete.CommandType = CommandType.Text;

                    cmdDelete.Parameters.Add("@RoleID", SqlDbType.Int).Value = domains[0].RoleID;

                    cmdDelete.Transaction = txn;
                    cmdDelete.ExecuteNonQuery(); // Delete Data
                    cmdDelete.Dispose();

                    domains.ForEach(delegate(recMenuRights domain)
                    {
                        SqlCommand cmdSave = conn.CreateCommand();
                        cmdSave.CommandText = "INSERT INTO [master].recMenuRights(MenuID, RoleID, AccessCode, CreatedDate, CreatedBy) VALUES(@MenuID, @RoleID, @AccessCode, @CreatedDate, @CreatedBy)";

                        cmdSave.Parameters.Add("@MenuID", SqlDbType.Int).Value = domain.MenuID;
                        cmdSave.Parameters.Add("@RoleID", SqlDbType.Int).Value = domain.RoleID;
                        cmdSave.Parameters.Add("@AccessCode", SqlDbType.VarChar).Value = (object)domain.AccessCode ?? DBNull.Value;
                        cmdSave.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.CreatedDate ?? DBNull.Value;
                        cmdSave.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.CreatedBy ?? DBNull.Value;

                        cmdSave.Transaction = txn;
                        cmdSave.ExecuteNonQuery(); // Save Data
                        cmdSave.Dispose();
                    });

                    result = true;
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }
            }

            return result;
        }
        public List<recAccessRights.AccessRights> GetAccessRights(int roleId, int menuId)
        {
            List<recAccessRights.AccessRights> tt = new List<recAccessRights.AccessRights>();

            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = " select AccessCode from master.recMenuRights where MenuID = @MenuID AND RoleID = @RoleID";
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@MenuID", SqlDbType.Int).Value = menuId;
                    cmd.Parameters.Add("@RoleID", SqlDbType.Int).Value = roleId;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            string code = string.Empty;
                            while (dr.Read())
                            {
                                code = dr[recMenuRights.Fields.AccessCode] == DBNull.Value ? (string)null : Convert.ToString(dr[recMenuRights.Fields.AccessCode]);
                                code = code.Trim();
                                recAccessRights.AccessRights accessRight = recAccessRights.AccessRights.NoAccess;

                                if (code.LastIndexOf('|') == (code.Length - 1))
                                    code = code.Remove(code.Length - 1); // Removes '|'

                                var codes = code.Split('|');
                                foreach (string item in codes)
                                {
                                    switch (item.ToLower().Trim())
                                    {
                                        case ("v"):
                                            accessRight = recAccessRights.AccessRights.View;
                                            break;
                                        case ("a"):
                                            accessRight = recAccessRights.AccessRights.Add;
                                            break;
                                        case ("e"):
                                            accessRight = recAccessRights.AccessRights.Edit;
                                            break;
                                        case ("d"):
                                            accessRight = recAccessRights.AccessRights.Delete;
                                            break;
                                        case ("r"):
                                            accessRight = recAccessRights.AccessRights.Review;
                                            break;
                                        case ("o"):
                                            accessRight = recAccessRights.AccessRights.Approve;
                                            break;
                                        case ("n"):
                                            accessRight = recAccessRights.AccessRights.Open;
                                            break;
                                    }
                                    tt.Add(accessRight);
                                }

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }

    }

}
